package edu.fyvtc.javaweb.demo.controller;


import edu.fyvtc.javaweb.demo.common.DemoResult;
import edu.fyvtc.javaweb.demo.common.ResultUtils;
import edu.fyvtc.javaweb.demo.model.House;
import edu.fyvtc.javaweb.demo.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/mybatis")
public class MybatisController {

    @Autowired
    private HouseService houseService;

    @RequestMapping("/insert")
    @ResponseBody
    public DemoResult insert(House house) {
        DemoResult demoResult;
        int affectedRows = houseService.insert(house);
        //如果插入行数大于0，则返回前端操作成功，否则返回操作失败
        if (affectedRows > 0) {
            demoResult = ResultUtils.success();
        } else {
            demoResult = ResultUtils.error();
        }
        return demoResult;
    }

    @RequestMapping("/selectById")
    @ResponseBody
    public DemoResult selectById(String id) {
        House house = houseService.selectById(id);
        DemoResult demoResult = ResultUtils.success(house);
        return demoResult;
    }

}
